package com.wl.sta.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wl.connonutils.R;
import com.wl.sta.client.UcenterClient;
import com.wl.sta.entity.StatisticsDaily;
import com.wl.sta.mapper.StatisticsDailyMapper;
import com.wl.sta.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author wl
 * @since 2021-09-07
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StatisticsDaily::getDateCalculated,day);
        this.remove(wrapper);
        R r = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) r.getData().get("count");
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);
        sta.setDateCalculated(day);
        sta.setCourseNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setGmtCreate(new Date());
        sta.setGmtModified(new Date());
        this.save(sta);
    }
}
